package com.udhtu.ii.lab2;


import java.util.Random;

import static com.udhtu.ii.lab2.Function.*;

public class SecondMethod {
    private double x;
    private double y;
    private double Xviv, Yviv, Inew;

    public SecondMethod() {
        this.x = X_FIRST;
        this.y = Y_FIRST;
        Xviv = X_FIRST;
        Yviv = Y_FIRST;
        Inew = 1.0;
    }

    private double random() {
        Random random = new Random();
        double number = random.nextDouble();

        if (number<5)
        return 1/number;
        else return 1/number*(-1);
    }

    public void secondMethod() {
        double Xrnd, Yrnd, fun, Xnew, Ynew;
        double h = 1.0, Fmin;
        fun = function(x, y);
        Fmin = fun;

        for (double i = 1.0; i <= 15.0; i++) {
            Xrnd = random();
            Yrnd = random();

            Xnew = x + h * Xrnd;
            Ynew = y + h * Yrnd;

            fun = function(Xnew, Ynew);
            if (fun < Fmin) {
                Fmin = fun;
                Inew = i;
                Xviv = Xnew;
                Yviv = Ynew;
            }
            x = Xnew;
            y = Ynew;
        }
    }

    public void compute() {
        this.secondMethod();
        System.out.println("Минимум функции в точке : [" + Xviv + ";" + Yviv + "]");
        System.out.println("Значение функции: " + function(Xviv, Yviv));
        System.out.println("Количество итераций по = " + Inew);
    }
}
